import { MessageBox,Message } from 'element-ui';
import Vue from '../main'
import {postRequest} from "@/utils/requestUtil";
import Store from "@/store";
import Router from "@/router";
import api from "@/api";

export default {
    logout(){
        postRequest(api.logout).then(res=>{
            //清除token和user信息
            sessionStorage.removeItem("token_login")
            sessionStorage.removeItem("user")
            // this.$store.state.subscribe.unsubscribe() //取消订阅频道
            //清除菜单列表
            Store.commit('resetState')
            Router.replace("/")
        })
    },
    oneConfirmDialog(name,thenFunction,suffix=''){
        const h = Vue.$createElement;
        return MessageBox({
            title: '提示',
            message: h('p', null, [
                h('span', null, "此操作将永久删除\t"),
                h('i', {style: 'color: teal'}, name),
                h('span', null, `\t${suffix}`),
            ]),
            showClose: false,
            showCancelButton: true,
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            beforeClose: (action, instance, done) => {
                if (action === 'confirm') {
                    instance.confirmButtonLoading = true;
                    instance.confirmButtonText = '执行中...';
                    setTimeout(() => {
                        done();
                        setTimeout(() => {
                            instance.confirmButtonLoading = false;
                        }, 300);
                    }, 500);
                } else {
                    done();
                }
            }
        }).then(thenFunction).catch(cancel => {
            Message({type: 'info', message: "已取消删除"});
        })
    }
}